/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.Map;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.business.Analyse_Data;
import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.db1.DBManager1;
import com.zxy.db10.DBManager10;
import com.zxy.db2.DBManager2;
import com.zxy.db3.DBManager3;
import com.zxy.db4.DBManager4;
import com.zxy.db5.DBManager5;
import com.zxy.db6.DBManager6;
import com.zxy.db7.DBManager7;
import com.zxy.db8.DBManager8;
import com.zxy.db9.DBManager9;
import com.zxy.db_Self.DBManager_Self;

public class SelfTimeTask extends java.util.TimerTask
{
	@Override
	public void run()
	{
		RunTask();
	}

	private void DbTimeOut()
	{
		// 60秒频率
		if(Com_Para.iTimeCount2 % 2 == 0)
		{
			// if(!DBManager_Self.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
			DBManager_Self.closeConnectionPoolTimeOut();
			if(DBManager1.bUse
					&& !java.util.Objects.equals(DBManager1.inst,null)
					&& !DBManager1.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager1.closeConnectionPoolTimeOut();
			if(DBManager2.bUse
					&& !java.util.Objects.equals(DBManager2.inst,null)
					&& !DBManager2.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager2.closeConnectionPoolTimeOut();
			if(DBManager3.bUse
					&& !java.util.Objects.equals(DBManager3.inst,null)
					&& !DBManager3.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager3.closeConnectionPoolTimeOut();
			if(DBManager4.bUse
					&& !java.util.Objects.equals(DBManager4.inst,null)
					&& !DBManager4.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager4.closeConnectionPoolTimeOut();
			if(DBManager5.bUse
					&& !java.util.Objects.equals(DBManager5.inst,null)
					&& !DBManager5.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager5.closeConnectionPoolTimeOut();
			if(DBManager6.bUse
					&& !java.util.Objects.equals(DBManager6.inst,null)
					&& !DBManager6.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager6.closeConnectionPoolTimeOut();
			if(DBManager7.bUse
					&& !java.util.Objects.equals(DBManager7.inst,null)
					&& !DBManager7.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager7.closeConnectionPoolTimeOut();
			if(DBManager8.bUse
					&& !java.util.Objects.equals(DBManager8.inst,null)
					&& !DBManager8.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager8.closeConnectionPoolTimeOut();
			if(DBManager9.bUse
					&& !java.util.Objects.equals(DBManager9.inst,null)
					&& !DBManager9.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager9.closeConnectionPoolTimeOut();
			if(DBManager10.bUse
					&& !java.util.Objects.equals(DBManager10.inst,null)
					&& !DBManager10.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				DBManager10.closeConnectionPoolTimeOut();
		}
	}

	private void RunTask()
	{
		if(!Com_Para.bTimeFlag && Com_Fun.flagSconnections())
		{
			Com_Para.bTimeFlag = true;
			Com_Para.iTimeCount2++;
			try
			{
				// 判断session
				SetSession();
				// 往服务端发送数据包
				// SendInfo();
				// 往客户端发送数据包
				SendClientInfo();
				// DB连接超时
				DbTimeOut();
				// 删除过期数据文件
				DelFileInfo();
				// 检测连接的子设备连接数
				// CheckServerSocket();
			}
			catch(Exception ee)
			{
			}
			Com_Para.bTimeFlag = false;
		}
	}

	private void SetSession()
	{
		if(Com_Para.iTimeCount2 % 2 == 0)
		{
			// 5分钟执行一次
			if(Com_Para.iTimeCount2 >= 10)
			{
				Com_Para.iTimeCount2 = 0;
				Ope_DB_Cent odc = new Ope_DB_Cent();
				// 接口权限认证
				odc.SetInterSession();
			}
			if(!java.util.Objects.equals(Com_Para.htSession,null))
			{
				for(Map.Entry<String, Object[]> entry: Com_Para.htSession.entrySet())
				{
					Object[] objV = entry.getValue();
					Date dtSession = new Date();
					try
					{
						dtSession = (Date) objV[1];
					}
					catch(Exception e)
					{
						Com_Fun.debug_in(e,this.toString());
					}
					long lgTime = new Date().getTime() - dtSession.getTime();
					if(lgTime > Com_Para.SessionTimeOut * 60000)// session有效判断7天
					{
						Com_Para.htSession.remove(entry.getKey());
					}
				}
			}
		}
	}

	private void CheckServerSocket()
	{
		if(Com_Para.iTimeCount2 % 2 == 0)
		{
			if(!java.util.Objects.equals(Com_Para.htServSockets,null))
			{
				for(Map.Entry<String, Socket> entry: Com_Para.htServSockets.entrySet())
				{
					Socket temSck = entry.getValue();
				}
			}
		}
	}

	private void SendClientInfo()
	{
		if(!java.util.Objects.equals(Com_Para.htServSockets,null)
				&& Com_Para.iTimeCount2 % 5 == 0)
		{
			for(Map.Entry<String, Socket> entry: Com_Para.htServSockets.entrySet())
			{
				String temKey = entry.getKey();
				Socket temSck = entry.getValue();
				// 发送数据包
				try
				{
					if(entry.getKey().indexOf("servSocket") != -1)
					{
						Analyse_Data ad = new Analyse_Data();
						ad.SendSocket(new StringBuilder(Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")+"\r\n"),temSck);
												
						if(java.util.Objects.equals(Com_Para.htServSockets.get(temKey),null))
							Com_Para.htServSockets.put(temKey,temSck);
					}
				}
				catch(Exception e)
				{
					try
					{
						temSck.close();
					}
					catch(Exception ev)
					{
						
					}
					Com_Fun.debug_in(e,temKey+this.toString());
					Com_Para.htServSockets.remove(temKey);
					temSck = null;
				}
			}
		}
	}

	private void SendInfo()
	{
		// 发送数据包
		if(!java.util.Objects.equals(Com_Para.clientSocket,null))
		{
			try
			{
				Analyse_Data ad = new Analyse_Data();
				ad.SendSocket(new StringBuilder("*"),Com_Para.clientSocket);
			}
			catch(Exception e)
			{
				Com_Fun.debug_in(e,this.toString());
			}
		}
	}

	@SuppressWarnings("deprecation")
	private void DelFileInfo()
	{
		// 删除数据文件
		if(new Date().getHours() == 2 && new Date().getMinutes() % 30 == 0)
		{
			String FilePath = Com_Para.ApplicationPath + Com_Para.zxyPath
					+ "data";
			File fiFile = new File(FilePath);
			if(fiFile.exists() && fiFile.isDirectory())
			{
				File[] fils = fiFile.listFiles();
				for(File fln: fils)
				{
					Date dtNow = new Date();
					dtNow.setDate(dtNow.getDate() - 1);// 1天前
					if(dtNow.compareTo(new Date(fln.lastModified())) == 1)
					{
						fln.delete();
					}
				}
			}
		}
	}
}